package com.groot.medical.service;

import java.util.List;

import com.groot.medical.dao.AdministrativeAreaDao;
import com.groot.medical.dao.AdministrativeAreaDaoImpl;
import com.groot.medical.dao.FamilyArchivesDao;
import com.groot.medical.dao.FamilyArchivesDaoImpl;
import com.groot.medical.dao.PersonalFileDao;
import com.groot.medical.dao.PersonalFileDaoImpl;
import com.groot.medical.entity.FamilyArchives;
import com.groot.medical.entity.PageBean;
import com.groot.medical.entity.PersonalFiles;

public class FamilyArchivesServiceImpl implements FamilyArchivesService {
	private FamilyArchivesDao familyArchivesDao = new FamilyArchivesDaoImpl();
	private AdministrativeAreaDao areaDao = new AdministrativeAreaDaoImpl();
	private PersonalFileDao personalFileDao = new PersonalFileDaoImpl();
	@Override
	public PageBean findByPage(int pageNum, int pageSize) {
		int totalRecord = familyArchivesDao.getAllFamilyCount();
		PageBean pageBean = new PageBean<>(pageNum, pageSize, totalRecord);
		int startIndex = pageBean.getStartIndex();
		pageBean.setList(familyArchivesDao.findFamilyByPage(startIndex,pageSize));
		return pageBean;
	}

	@Override
	public int addFamilyInfo(FamilyArchives family) {
		//自动生产家庭编号
		String familyCode= familyArchivesDao.aotuCreateFamilyCode(family);
		family.setFamilyCode(familyCode);
		//生产家庭政治
		int i = familyArchivesDao.addFamilyInfo(family);
		
		return i;
	}

	@Override
	public FamilyArchives findFamilyInfoByFamilyCode(String familyCode) {
		// TODO Auto-generated method stub
		return familyArchivesDao.findFamilyInfoByFamilyCode(familyCode);
	}

	@Override
	public List<String> findAreaName(FamilyArchives family) {
		List<String> findAreaName=areaDao.findAreaNameBycode(family.getTownCode(),family.getContryCode(),family.getVillageCode(),family.getGroupCode());
		return findAreaName;
	}

	@Override
	public List<PersonalFiles> findFamilyAllPersonByFamilyCode(String familyCode) {
		List<PersonalFiles> persons = familyArchivesDao.findFamilyAllPersonByFamilyCode(familyCode);
		return persons;
	}

	@Override
	public List<FamilyArchives> findFamilyListByFamilyCode(String familyCode) {
		List<FamilyArchives> familys = familyArchivesDao.findFamilyListByFamilyCode(familyCode);
		return familys;
	}

	@Override
	public PageBean findByPageAndLike(int pageNum, int pageSize, String holdername, String townCode) {
		int totalRecord = familyArchivesDao.getAllFamilyCountByLike(holdername,townCode);
		PageBean pageBean = new PageBean<>(pageNum, pageSize, totalRecord);
		int startIndex = pageBean.getStartIndex();
		pageBean.setList(familyArchivesDao.findByPageAndLike(startIndex,pageSize,townCode,holdername));
		return pageBean;
	}

	@Override
	public void ediFamilyInfo(FamilyArchives family) {
		// TODO Auto-generated method stub
		familyArchivesDao.ediFamilyInfo(family);
	}

	@Override
	public int deleteFamily(String familyCode) {
		
		int i=familyArchivesDao.deleteFamily(familyCode);
		if(i>0){
			personalFileDao.delPersonInfo(familyCode);
		}
		return i;
	}

	

}
